namespace PickGold.Charting
{
    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.Reflection;

    internal class AxisLabelDateValueConverter : DoubleConverter
    {
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            object obj2 = null;
            DateTime time;
            bool flag = false;
            string s = value as string;
            if ((context != null) && (context.Instance != null))
            {
                DateTimeIntervalType auto = DateTimeIntervalType.Auto;
                PropertyInfo property = context.Instance.GetType().GetProperty("intervalType");
                if (property != null)
                {
                    auto = (DateTimeIntervalType) property.GetValue(context.Instance, null);
                }
                if (auto == DateTimeIntervalType.Auto)
                {
                    Axis instance = null;
                    if (context.Instance is Axis)
                    {
                        instance = (Axis) context.Instance;
                    }
                    else
                    {
                        MethodInfo method = context.Instance.GetType().GetMethod("GetAxis");
                        if (method != null)
                        {
                            instance = (Axis) method.Invoke(context.Instance, null);
                        }
                    }
                    if (instance != null)
                    {
                        auto = instance.GetAxisIntervalType();
                    }
                }
                if (((s != null) && (auto != DateTimeIntervalType.Number)) && (auto != DateTimeIntervalType.Auto))
                {
                    flag = true;
                }
            }
            try
            {
                obj2 = base.ConvertFrom(context, culture, value);
            }
            catch (ArgumentException)
            {
                obj2 = null;
            }
            catch (NotSupportedException)
            {
                obj2 = null;
            }
            if (((s != null) && (flag || (obj2 == null))) && DateTime.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.None, out time))
            {
                return time.ToOADate();
            }
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (((context != null) && (context.Instance != null)) && (destinationType == typeof(string)))
            {
                DateTimeIntervalType auto = DateTimeIntervalType.Auto;
                double num = 0.0;
                PropertyInfo property = context.Instance.GetType().GetProperty("IntervalType");
                if (property != null)
                {
                    auto = (DateTimeIntervalType) property.GetValue(context.Instance, null);
                }
                property = context.Instance.GetType().GetProperty("Interval");
                if (property != null)
                {
                    num = (double) property.GetValue(context.Instance, null);
                }
                if (auto == DateTimeIntervalType.Auto)
                {
                    Axis instance = null;
                    if (context.Instance is Axis)
                    {
                        instance = (Axis) context.Instance;
                    }
                    else
                    {
                        MethodInfo method = context.Instance.GetType().GetMethod("GetAxis");
                        if (method != null)
                        {
                            instance = (Axis) method.Invoke(context.Instance, null);
                        }
                    }
                    if (instance != null)
                    {
                        auto = instance.GetAxisIntervalType();
                    }
                }
                if ((context.Instance.GetType() != typeof(StripLine)) || (num == 0.0))
                {
                    switch (auto)
                    {
                        case DateTimeIntervalType.Number:
                        case DateTimeIntervalType.Auto:
                            goto Label_0141;
                    }
                    if (auto < DateTimeIntervalType.Hours)
                    {
                        return DateTime.FromOADate((double) value).ToShortDateString();
                    }
                    return DateTime.FromOADate((double) value).ToString("g", CultureInfo.CurrentCulture);
                }
            }
        Label_0141:
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }
}

